package com.chuangke.sinotrukbudget.flow;

import java.util.List;
import java.util.stream.Collectors;

import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.chuangke.admin.model.SysDataPermission;
import com.chuangke.admin.service.SysDataPermissionService;
import com.chuangke.common.SpringContextUtil;
import com.chuangke.core.exception.ChuangkeException;
import com.chuangke.flow.constant.FlowConstant;
import com.chuangke.flow.domain.OrderModel;
import com.chuangke.flow.model.BaseNodeProcessor;
import com.chuangke.flow.model.FlowRuNode;
import com.chuangke.flow.model.FlowRuNodeProcessor;
import com.chuangke.flow.plugin.FlowGetTaskUser;
import com.chuangke.sinotrukbudget.model.BudgetBpcPlan;
import com.chuangke.sinotrukbudget.model.BudgetDeptAccount;
import com.chuangke.sinotrukbudget.sdao.BudgetBpcPlanDaoService;
import com.chuangke.sinotrukbudget.service.BudgetDeptAccountService;

@Service("budgetFlowGetTaskUser")
public class BudgetFlowGetTaskUser implements FlowGetTaskUser {

    @Autowired
    private BudgetDeptAccountService budgetDeptAccountService;
    @Autowired
    private SysDataPermissionService sysDataPermissionService;
    @Autowired
    private BudgetBpcPlanDaoService budgetBpcPlanDaoService;

    @Override
    public List<String> getTaskUser(FlowRuNode node, FlowRuNodeProcessor nodeProcessor, OrderModel orderModel) {
        if (!BaseNodeProcessor.PROCESS_TYPE_ROLE.equals(nodeProcessor.getProcessType())) {
            throw new ChuangkeException(String.format("当前节点[id：{},title:{}]只支持处理人为角色", node.getId(), node.getTitle()));
        }
		
		/*
		  @第一步根据预算科目找到可以审批的部门;
		 * @第二步根据部门到用户表找到这个部门下面的人 ;
		 * @第三步根据根据用户和角色找到有这个单位审批权限的用户;
		 * @第四步将这两个用户列表做个并集就是可以审批这个单子的所有人

		 */
        // 1、defaultFlowGetTaskUser根据当前节点的角色和同时有这个角色并且可以审批这个单位数据的权限找到可以审批的用户列表
        FlowGetTaskUser sysDeptService = SpringContextUtil.getBean(FlowConstant.DEFAULT_FLOW_GET_TASK_USER_SERVICE,
                FlowGetTaskUser.class);
        List<String> taskUserList1 = sysDeptService.getTaskUser(node, nodeProcessor, orderModel);

        // 剩下的审核是归口领导审核，需要找归口对应的科目
        List<String> taskUserList2 = getAuditUserByAccountDept(nodeProcessor, orderModel);

        // 获取并集
        List<String> taskUserList = taskUserList1.stream().filter(taskUserList2::contains)
                .collect(Collectors.toList());

        if (CollectionUtils.isEmpty(taskUserList)) {
            BudgetBpcPlan bbp = budgetBpcPlanDaoService.getById(orderModel.getOrderId());

            throw new ChuangkeException(String.format("公司[%s]下的科目[%s]在审批节点[%s]下未找到审批人，请确认科目是否维护归口及相应的审批人",
                    bbp.gettEntity(), bbp.gettAccount(), node.getTitle()));
        }

        return taskUserList;
    }

    private List<String> getAuditUserByAccountDept(FlowRuNodeProcessor nodeProcessor, OrderModel orderModel) {
        List<BudgetDeptAccount> daList = budgetDeptAccountService
                .listByAccount((String) orderModel.getVariables().get("t_account"));
        List<String> deptList = daList.stream().map(BudgetDeptAccount::getDeptId).collect(Collectors.toList());
        List<SysDataPermission> sdpList = sysDataPermissionService.find(SysDataPermission.TYPE_USER_DUTY);

        // 用户职能权限
        String roleId = nodeProcessor.getProcessId();
        return sdpList.stream().filter(sdp -> deptList.contains(sdp.getDataId()) && roleId.equals(sdp.getMaster2Id()))
                .map(SysDataPermission::getMaster1Id).collect(Collectors.toList());
    }

}
